package com.examsystem.controller;

import com.alibaba.fastjson.JSON;
import com.examsystem.Const.ConstMsg;
import com.examsystem.pojo.Answer;
import com.examsystem.pojo.vo.AnswerVo;
import com.examsystem.service.AnswerService;
import com.examsystem.pojo.vo.AnswerCards;
import com.examsystem.pojo.vo.LoginUser;
import com.examsystem.pojo.vo.Result;
import com.examsystem.util.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import java.util.List;

import static com.examsystem.Const.ConstMsg.STATUS_CODE;

/**
 * @author fire'ge
 */
@Controller
@RequestMapping(value = "/answer")
public class AnswerController {
    @Resource
    private AnswerService answerService;

    /*
    学生端代码
     */

    /**
     *通过jwt获取到用户的userid，然后以json格式来获取到answer类，添加到answer表中，表示已成功填写了答案
     * @param answer answer类，前端以json格式来传入
     * @return 返回result信息
     */
    @PreAuthorize("hasAuthority('answer:addToList')")
    @RequestMapping(value = "/addToList",produces = "application/json;charset=utf-8")
    public String addToList(@RequestBody Answer answer) {
        LoginUser user = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        answer.setAnswerUser(user.getUser().getUserId());
        //将答案写入到数据库中
        int add = answerService.addAnswer(answer);
        return JSON.toJSONString(Result.succ(STATUS_CODE,ConstMsg.ADD_SUCCESS_MSG,add));
    }

    /**
     * 根据paperId和token提供的userId来查看学生的试卷的答题情况，得到了多少分
     * @param paperId 根据paperId来获取答题卡情况
     * @return 返回答题卡的json信息
     */

    @PreAuthorize("hasAuthority('answer:queryAnswerCard')")
    @ResponseBody
    @RequestMapping(value = "/queryAnswerCard/{paperId}",produces = "application/json;charset=utf-8")
    public String queryAnswerCards(@PathVariable int paperId, HttpServletRequest request) {
        String token = request.getHeader("token");
        int jwtUserId = JwtUtil.getJWTUserId(token);
        List<AnswerCards> answerCards = answerService.queryAnswerCard(jwtUserId, paperId);
        return JSON.toJSONString(answerCards);
    }


/*
教师端代码
 */

    /**
     * 显示学生对这道题的回答【填空】
     * @param paperId  试卷id
     * @param questionId  试题id
     * @param classId  班级id
     * @param begin  开始【下标】
     * @return  result结果集对象
     */
//    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/answerList/{paperId}/{questionId}/{classId}/{begin}")
    @ResponseBody
    public Result answerList(@PathVariable Integer paperId,@PathVariable Integer questionId,@PathVariable Integer classId,@PathVariable Integer begin){
        return Result.succ(answerService.showStudentAnswer(paperId, classId, questionId, begin));
    }

    /**
     * 评分
     * @param choice 选择【1：手动  2：自动】
     * @param point  分数
     * @param answerId  回答id
     * @param paperId  试卷id
     * @param questionId  试题id
     * @param classId  班级id
     * @return  result结果集对象
     */
//    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/giveMark/{choice}/{point}/{answerId}/{paperId}/{questionId}/{classId}")
    @ResponseBody
    public Result giveMark(@PathVariable Integer choice,@PathVariable Double point,@PathVariable Integer answerId,@PathVariable Integer paperId,@PathVariable Integer questionId,@PathVariable Integer classId){
        return answerService.giveMark(choice, point, answerId, paperId, questionId,classId);
    }
}
